using System;
using System.Collections.Generic;
using System.Text;

namespace EuropeEngulfed.NET
{
    class FleetPoint : PlayingPiece
    {
        private bool withoutTransport;

        public bool WithoutTransport
        {
            get
            {
                return withoutTransport;
            }

            set
            {
                withoutTransport = value;
            }
        }

        public override MapInfo.CountryID CountryOwner
        {
            get
            {
                if (this.countryOwner == MapInfo.CountryID.USA)
                    return MapInfo.CountryID.GreatBritain;
                else
                    return this.countryOwner;
            }

            set
            {
                countryOwner = value;
            }
        }

        public FleetPoint(MapInfo.CountryID countryOwner) : base(countryOwner)
        {
            switch (this.CountryOwner)
            {
                case MapInfo.CountryID.GreatBritain:
                case MapInfo.CountryID.USA:
                case MapInfo.CountryID.France:
                case MapInfo.CountryID.Germany:
                case MapInfo.CountryID.Italy:
                case MapInfo.CountryID.USSR:
                    break;
                default:
                    throw new Exception("Attempted to create Invalid fleet point");
            }
            withoutTransport = false;
        }

        public override bool Equals(PlayingPiece playingPiece)
        {
            if (playingPiece is FleetPoint)
            {
                if (this.CountryOwner == playingPiece.CountryOwner)
                    return true;
            }

            return false;
        }

        public override string GetDescription()
        {
            return Enum.GetName(typeof(MapInfo.CountryID),this.CountryOwner) + " Fleet Point";
        }

        public System.Xml.XmlNode Serialise(System.Xml.XmlDocument xml)
        {
            System.Xml.XmlNode nodeToReturn = xml.CreateElement("FleetPoint");
            System.Xml.XmlAttribute countryAttr = xml.CreateAttribute("Country");
            System.Xml.XmlAttribute withoutTransAttr = xml.CreateAttribute("WithoutTransport");
            countryAttr.Value = Enum.GetName(typeof(MapInfo.CountryID), this.countryOwner);
            withoutTransAttr.Value = withoutTransport.ToString();
            nodeToReturn.Attributes.Append(countryAttr);
            nodeToReturn.Attributes.Append(withoutTransAttr);

            return nodeToReturn;
        }

        public new static FleetPoint Deserialise(System.Xml.XmlNode node)
        {
            FleetPoint fleetToReturn = null;
            if(node.Name == "FleetPoint")
            {
                fleetToReturn = new FleetPoint((MapInfo.CountryID)Enum.Parse(typeof(MapInfo.CountryID), node.Attributes["Country"].Value));
                fleetToReturn.withoutTransport = bool.Parse(node.Attributes["WithoutTransport"].Value);
            }

            return fleetToReturn;
        }

        public bool Equals(FleetPoint fleetToCompare)
        {
            if (this.countryOwner != fleetToCompare.countryOwner)
                return false;
            if (this.OutOfSupply != fleetToCompare.OutOfSupply)
                return false;
            if (this.withoutTransport != fleetToCompare.withoutTransport)
                return false;
            return true;
        }

        public new FleetPoint Copy()
        {
            FleetPoint fleetToCopy = new FleetPoint(this.CountryOwner);
            fleetToCopy.OutOfSupplyState = this.OutOfSupplyState;
            fleetToCopy.WithoutTransport = this.withoutTransport;
            return fleetToCopy;
        }

        public override bool CanDoDoubleMovement()
        {
            return false;
        }

        public override string GetHiddenStringDescription()
        {
            return MapInfo.GetCountryAdjective(this.countryOwner) + " fleet";
        }
    }
}
